<template>
  <div class="user-detail">
    <el-form
      ref="userFormRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      :disabled="type === 'view'"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="用户ID" prop="id">
            <el-input v-model="formData.id" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="formData.username" placeholder="请输入用户名" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="formData.email" placeholder="请输入邮箱" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="formData.phone" placeholder="请输入手机号" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="角色" prop="role">
            <el-select v-model="formData.role" placeholder="请选择角色">
              <el-option label="普通用户" value="user" />
              <el-option label="管理员" value="admin" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="formData.status">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-form-item label="创建时间">
        <el-input v-model="formData.createTime" disabled />
      </el-form-item>
      
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="formData.remark"
          type="textarea"
          :rows="4"
          placeholder="请输入备注信息"
        />
      </el-form-item>
    </el-form>
    
    <div class="form-actions" v-if="type === 'edit'">
      <el-button type="primary" @click="handleSave">保存</el-button>
      <el-button @click="handleReset">重置</el-button>
      <el-button @click="handleClose">关闭</el-button>
    </div>
    
    <div class="form-actions" v-else>
      <el-button type="primary" @click="handleEdit">编辑</el-button>
      <el-button @click="handleClose">关闭</el-button>
    </div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus'

export default {
  name: 'UserDetail',
  props: {
    data: {
      type: Object,
      required: true
    },
    type: {
      type: String,
      default: 'view' // view, edit
    }
  },
  data() {
    return {
      formData: {
        id: '',
        username: '',
        email: '',
        phone: '',
        role: 'user',
        status: 1,
        createTime: '',
        remark: ''
      },
      formRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        role: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    this.initFormData()
  },
  methods: {
    initFormData() {
      this.formData = { ...this.data }
      if (!this.formData.remark) {
        this.formData.remark = ''
      }
    },
    
    handleSave() {
      this.$refs.userFormRef.validate((valid) => {
        if (valid) {
          this.$emit('save', this.formData)
          ElMessage.success('保存成功')
        }
      })
    },
    
    handleReset() {
      this.initFormData()
      ElMessage.info('已重置表单')
    },
    
    handleEdit() {
      // 切换到编辑模式
      const currentRoute = this.$route
      this.$router.push({
        path: currentRoute.path,
        query: {
          ...currentRoute.query,
          action: 'edit'
        }
      })
    },
    
    handleClose() {
      this.$emit('close', this.$route.query.type + '-' + this.$route.query.id + '-' + this.$route.query.action)
    }
  },
  
  watch: {
    data: {
      handler(newData) {
        this.initFormData()
      },
      deep: true
    }
  }
}
</script>

<style scoped>
.user-detail {
  max-width: 800px;
}

.form-actions {
  margin-top: 30px;
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid #e4e7ed;
}

.form-actions .el-button {
  margin: 0 10px;
}

.el-form-item {
  margin-bottom: 20px;
}
</style>